package launcher.simple.android.woblog.cn.system;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import launcher.simple.android.woblog.cn.simplelauncher.R;

public class CallInfo {
	private int mPhoneState = PhoneState.STATE_IDLE;
	private String mRemoteName;
	private String mRemoteNumber;
	private PhoneType mType = PhoneType.MOBILE;
	private OnPhoneStateChangedListener mListener;
	private static final String TAG = "CallInfo";

	public CallInfo(Context context, String name, String number) {
		mType = getNumberType(number);
		mRemoteNumber = number;
		mRemoteName = TextUtils.isEmpty(name) ? queryName(context) : name;
	}

	public int getPhoneState() {
		return mPhoneState;
	}

	public void setPhoneState(int state) {
		if (mPhoneState == state) {
			return;
		}
		this.mPhoneState = state;
		if (mListener != null) {
			mListener.onPhoneStateChanged(state);
		}
	}

	public PhoneType getPhoneType() {
		return mType;
	}

	public String getRemoteName() {
		return mRemoteName;
	}

	public String getRemoteNumber() {
		return mRemoteNumber;
	}

	public void clean() {
		setPhoneState(PhoneState.STATE_IDLE);
		mRemoteName = null;
		mRemoteNumber = null;
	}

	public void setOnPhoneStateChangedListener(OnPhoneStateChangedListener l) {
		this.mListener = l;
	}

	public interface OnPhoneStateChangedListener {
		void onPhoneStateChanged(int state);
	}

	public static PhoneType getNumberType(String number) {
		if (number == null) {
			return PhoneType.PUBLIC;
		}
		if (number.startsWith("+")) {
			return PhoneType.INTERNATIONAL;
		} else if (number.length() == 11
				&& (number.startsWith("13") || number.startsWith("15") || number
						.startsWith("18"))) {
			return PhoneType.MOBILE;
		} else if (number.length() == 7 || number.length() == 8 || number.startsWith("0")) {
			return PhoneType.PHONE;
		} else {
			return PhoneType.PUBLIC;
		}
	}

	private String configNumber() {
	    if (mRemoteNumber == null) {
            return mRemoteNumber;
        }
		String number = mRemoteNumber.replaceAll("[^0-9]", "");
		switch (mType) {
		case PHONE:
			if (number.length() > 8) {
				if (number.startsWith("01") || number.startsWith("02")) {
					number = number.substring(3);
				} else {
					number = number.substring(4);
				}
			}
			break;
		case INTERNATIONAL:
			if (number.length() > 11) {
				number = number.substring(number.length() - 11);
			}
			break;
		case MOBILE:
		case PUBLIC:
		default:
			break;
		}
		return number;
	}

	private String queryName(Context context) {
		String number = configNumber();
		if (number == null) {
            return context.getResources().getString(R.string.unknow);
        }
		Log.i(TAG, "getName:" + number);
		String[] projection = { "contact_id", "data1" };
		StringBuffer sb = new StringBuffer(number);
		if (number.length() > 4) {
			for (int i = number.length() - 4; i > 0; i--) {
				sb = sb.insert(i, "%");
			}
			if (mType == PhoneType.PHONE || mType == PhoneType.INTERNATIONAL) {
				sb = sb.insert(0, "%");
			}
		}

		Cursor cursor = context
				.getContentResolver()
				.query(Uri.withAppendedPath(
						Uri.parse("content://cn.ingenic.contactslite"), "data"),
						projection, " data1 like '" + sb.toString() + "'",
						null, null);

		if (cursor == null) {
			Log.e(TAG, "getContactId null");
			return null;
		}
		int contactId = -1;
		if (cursor.moveToFirst()) {
			do {
				String num = cursor.getString(cursor.getColumnIndex("data1"));
				num = num.replaceAll("[^0-9]", "");
				Log.i("num", "num:" + num);
				if (num.equals(number)) {
					int columnIndex = cursor.getColumnIndex("contact_id");
					contactId = cursor.getInt(columnIndex);
					break;
				} else if (mType == PhoneType.PHONE || mType == PhoneType.INTERNATIONAL) {
					if ((num.length() > number.length() && num.endsWith(number))
							|| (number.length() > num.length() && number
									.endsWith(num))) {
						int columnIndex = cursor.getColumnIndex("contact_id");
						contactId = cursor.getInt(columnIndex);
						break;
					}
				}
			} while (cursor.moveToNext());
		}
		cursor.close();
		if (contactId == -1) {
			Log.e(TAG, "id not got");
			return null;
		}
		Cursor cursor2 = context
				.getContentResolver()
				.query(Uri.withAppendedPath(
						Uri.parse("content://cn.ingenic.contactslite"), "data"),
						projection,
						" contact_id = '"
								+ contactId
								+ "' AND mimetype = 'vnd.android.cursor.item/name'",
						null, null);
		if (cursor2 == null) {
			Log.e(TAG, "getName null");
			return null;
		}
		String name = null;
		if (cursor2.moveToFirst()) {
			int columnIndex = cursor2.getColumnIndex("data1");
			name = cursor2.getString(columnIndex);
		}
		cursor2.close();
		return name;
	}

	public enum PhoneType {
		PHONE, MOBILE, PUBLIC, INTERNATIONAL
	}
}
